<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_session.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_outside_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';
$title = 'Register New Account';

$first_name = "";
$last_name = "";
$phone_number = "";
$email = "";
$password = "";
$password_confirm = "";


if (!empty($_POST)) {
    if (!empty($_POST['txt_first_name'])) {
        $first_name = $_POST['txt_first_name'];
    }
    if (!empty($_POST['txt_last_name'])) {
        $last_name = $_POST['txt_last_name'];
    }
    if (!empty($_POST['txt_phone_number'])) {
        $phone_number = $_POST['txt_phone_number'];
    }
    if (!empty($_POST['txt_email'])) { 
        $email = $_POST['txt_email'];
    }
    if (!empty($_POST['txt_password'])) {
        $password = $_POST['txt_password'];
    }
    if (!empty($_POST['txt_password_confirm'])) {
        $password_confirm = $_POST['txt_password_confirm'];
    }

    $error_show = api_security::validate_register($first_name, $last_name,
            $phone_number, $email, $password, $password_confirm);
    if (empty($error_show)) {
        $activate_code = api_security::register($email, $password, $last_name, $first_name, $phone_number);
        
        if (!empty($activate_code)) {
            $email_sent_to   = $email;
            $url_to_activate = LINK_ROOT . "/verify_account.php?email=$email&activate_code=$activate_code";
            $message         = "Recently, you have registered the email: $email. Please go here to activate you account: $url_to_activate \n";
            mail($email_sent_to, "Confirm Registration | Movie Websites", $message);
            $success_info = "Congratulations, Register Successfully, Please go to your email $email to active your account.";
        }
        $first_name = "";
        $last_name = "";
        $phone_number = "";
        $email = "";
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="ts-body-container">
            <div class="ts-wrapper">
                <div class="padding-top-large row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php';
                    ?>
                    <div class="ui segment breadcrumb" style="margin-top: 25px; margin-left: 20px;">
                        <a class="section" href="index.php">Home</a>
                        <i class="right arrow icon divider"></i>
                        <a class="active section">Register</a>
                    </div>
                </div>
                <div class="ui grid margin-top">
                    <div class="row">
                        <div class="column">
                            <h1>Register Now</h1>
                            <form name="form_register" id="form_register" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                                <?php if (!empty($error_show)) { ?>
                                    <div class="ui error message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            Please correct these wrongs.
                                        </div>
                                        <ul class="list">
                                            <?php echo $error_show; ?>
                                        </ul>
                                    </div>
                                <?php } else if (!empty($success_info)) {
                                    ?>
                                    <div class="ui success message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            <?php echo $success_info; ?>
                                        </div>
                                    </div>
                                    <?php
                                }
                                ?>
                                <div class="ui form segment">
                                    <div class="two fields">
                                        <div class="field">
                                            <label for="FirstName">First Name</label>
                                            <input id="txt_first_name" name="txt_first_name" class="small input" value="<?php echo $first_name; ?>" placeholder="First Name" type="text" />
                                        </div>

                                        <div class="field">
                                            <label for="LastName">Last Name</label>
                                            <input id="txt_last_name" name="txt_last_name" value="<?php echo $last_name; ?>" placeholder="Last Name" type="text">
                                        </div>
                                    </div>

                                    <div class="field">
                                        <label for="PhoneNumber">Phone Number (please fill correctly, we'll use this to verify your booking)</label>
                                        <input type="text" placeholder="e.g: 0988549089" id="txt_phone_number" name="txt_phone_number" value="<?php echo $phone_number; ?>">
                                    </div>

                                    <div class="field">
                                        <label for="Email">Email (please fill correctly, we'll use this to verify your booking)</label>
                                        <input id="txt_email" name="txt_email" value="<?php echo $email; ?>" placeholder="e.g: your_name@example.com" type="text">
                                    </div>

                                    <div class="field">
                                        <label for="Password">Password</label>
                                        <input id="txt_password" name="txt_password" type="password">
                                    </div>

                                    <div class="field">
                                        <label for="PasswordConfirm">Password Confirm</label>
                                        <input id="txt_password_confirm" name="txt_password_confirm" value="" type="password">
                                    </div>
                                    <div class="large animated fade ui orange button" onclick="submitRegister();">
                                        <div class="visible content">Submit</div>
                                        <div class="hidden content"><i class="ok sign icon"></i></div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
                    ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>